import { CrudOptions, AddReq, DelReq, EditReq, dict, CrudExpose, UserPageQuery, CreateCrudOptionsRet } from '@fast-crud/fast-crud';
import * as api from './api';
import { auth } from "/@/utils/authFunction";
import dayjs from 'dayjs';

export default function ({ crudExpose }: { crudExpose: CrudExpose }): CreateCrudOptionsRet {
    const pageRequest = async (query: any) => {
        const { start_date, end_date, ...restQuery } = query;
        let dateQuery = {};
        
        if (start_date && end_date) {
            dateQuery = {
                start_date: start_date,
                end_date: end_date
            };
        } else if (start_date) {
            dateQuery = {
                start_date: start_date
            };
        } else if (end_date) {
            dateQuery = {
                end_date: end_date
            };
        }

        return await api.GetList({
            ...restQuery,
            ...dateQuery
        });
    };
    const editRequest = async ({ form, row }: EditReq) => {
        if (row.id) {
            form.id = row.id;
        }
        return await api.UpdateObj(form);
    };
    const delRequest = async ({ row }: DelReq) => {
        return await api.DelObj(row.id);
    };
    const addRequest = async ({ form }: AddReq) => {
        return await api.AddObj(form);
    };

    const exportRequest = async (query: UserPageQuery) => {
        return await api.exportData(query)
    };

    return {
        crudOptions: {
            request: {
                pageRequest,
                addRequest,
                editRequest,
                delRequest,
            },
            actionbar: {
                buttons: {
                    export: {
                        show: auth('ProductionPlan:Export'),
                        text: "导出",
                        title: "导出",
                        click() {
                            return exportRequest(crudExpose.getSearchFormData())
                        }
                    },
                    add: {
                        show: auth('ProductionPlan:Create'),
                    },
                }
            },
            table: {
                show: auth('ProductionPlan:Search'),
            },
            rowHandle: {
                fixed: 'right',
                width: 200,
                buttons: {
                    view: {
                        type: 'text',
                        order: 1,
                        show: auth('ProductionPlan:Retrieve')
                    },
                    edit: {
                        type: 'text',
                        order: 2,
                        show: auth('ProductionPlan:Update')
                    },
                    copy: {
                        type: 'text',
                        order: 3,
                        show: auth('ProductionPlan:Copy')
                    },
                    remove: {
                        type: 'text',
                        order: 4,
                        show: auth('ProductionPlan:Delete')
                    },
                },
            },
            columns: {
                plan_name: {
                    title: '计划名称',
                    type: 'input',
                    search: { 
                        show: true,
                        component: {
                            placeholder: '请输入计划名称',
                        }
                    },
                    column: {
                        minWidth: 120,
                        sortable: 'custom',
                    },
                    form: {
                        helper: {
                            render() {
                                return <div style={"color:blue"}>计划名称必须填写</div>;
                            }
                        },
                        rules: [{ required: true, message: '计划名称必填' }],
                        component: {
                            placeholder: '请输入计划名称',
                        },
                    },
                },
                product: {
                    title: '生产产品',
                    type: 'input',
                    search: { 
                        show: true,
                        component: {
                            placeholder: '请输入生产产品',
                        }
                    },
                    column: {
                        minWidth: 120,
                        sortable: 'custom',
                    },
                    form: {
                        helper: {
                            render() {
                                return <div style={"color:blue"}>生产产品必须填写</div>;
                            }
                        },
                        rules: [{ required: true, message: '生产产品必填' }],
                        component: {
                            placeholder: '请输入生产产品',
                        },
                    },
                },
                plan_quantity: {
                    title: '计划数量',
                    type: 'number',
                    search: { show: false },
                    column: {
                        minWidth: 100,
                        sortable: 'custom',
                    },
                    form: {
                        rules: [{ required: true, message: '计划数量必填' }],
                        component: {
                            placeholder: '请输入计划数量',
                            min: 1,
                        },
                    },
                },
                actual_quantity: {
                    title: '实际完成数量',
                    type: 'number',
                    search: { show: false },
                    column: {
                        minWidth: 120,
                        sortable: 'custom',
                    },
                    form: {
                        component: {
                            placeholder: '请输入实际完成数量',
                            min: 0,
                        },
                    },
                },
                start_date: {
                    title: '开始日期',
                    type: 'date',
                    search: { 
                        show: true,
                        component: {
                            format: "YYYY-MM-DD",
                            valueFormat: "YYYY-MM-DD",
                            placeholder: '开始日期'
                        }
                    },
                    form: {
                        rules: [
                            { required: true, message: '开始日期必填' },
                            {
                                validator: (rule: any, value: any, callback: any) => {
                                    const form = crudExpose.getFormData();
                                    if (form.end_date && value && dayjs(value).isAfter(form.end_date)) {
                                        callback(new Error('开始日期不能晚于结束日期'));
                                    } else {
                                        callback();
                                    }
                                }
                            }
                        ],
                        component: {
                            format: "YYYY-MM-DD",
                            valueFormat: "YYYY-MM-DD",
                            placeholder: '请选择开始日期',
                        }
                    },
                    column: {
                        align: "center",
                        width: 120,
                        component: { name: "fs-date-format", format: "YYYY-MM-DD" }
                    }
                },
                end_date: {
                    title: '结束日期',
                    type: 'date',
                    search: { 
                        show: true,
                        component: {
                            format: "YYYY-MM-DD",
                            valueFormat: "YYYY-MM-DD",
                            placeholder: '结束日期'
                        }
                    },
                    form: {
                        rules: [
                            { required: true, message: '结束日期必填' },
                            {
                                validator: (rule: any, value: any, callback: any) => {
                                    const form = crudExpose.getFormData();
                                    if (form.start_date && value && dayjs(value).isBefore(form.start_date)) {
                                        callback(new Error('结束日期不能早于开始日期'));
                                    } else {
                                        callback();
                                    }
                                }
                            }
                        ],
                        component: {
                            format: "YYYY-MM-DD",
                            valueFormat: "YYYY-MM-DD",
                            placeholder: '请选择结束日期',
                        }
                    },
                    column: {
                        align: "center",
                        width: 120,
                        component: { name: "fs-date-format", format: "YYYY-MM-DD" }
                    }
                },
                status: {
                    title: '计划状态',
                    type: 'dict-select',
                    dict: dict({
                        data: [
                            { value: 1, label: '未开始' },
                            { value: 2, label: '进行中' },
                            { value: 3, label: '已完成' },
                            { value: 4, label: '已取消' }
                        ]
                    }),
                    search: { show: true },
                    column: {
                        width: 100,
                        sortable: 'custom',
                    },
                    form: {
                        rules: [{ required: true, message: '状态必选' }],
                        component: {
                            placeholder: '请选择状态',
                        },
                    },
                },
                priority: {
                    title: '优先级',
                    type: 'number',
                    search: { show: false },
                    column: {
                        width: 80,
                        sortable: 'custom',
                    },
                    form: {
                        value: 1,
                        component: {
                            placeholder: '请输入优先级',
                            min: 1,
                        },
                    },
                },
                remarks: {
                    title: '备注',
                    type: 'textarea',
                    search: { show: false },
                    column: {
                        minWidth: 150,
                    },
                    form: {
                        component: {
                            placeholder: '请输入备注信息',
                            showWordLimit: true,
                            maxlength: 500,
                            rows: 3,
                        },
                    },
                },
            },
        },
    };
} 